package org.artifact.core.plugin.mongo;

import cn.hutool.core.map.MapUtil;
import cn.hutool.db.nosql.mongo.MongoDS;
import cn.hutool.db.nosql.mongo.MongoFactory;
import cn.hutool.setting.Setting;
import com.mongodb.client.MongoDatabase;
import org.artifact.core.lang.IPlugin;
import org.artifact.core.lang.IServer;
import org.artifact.core.lang.MapPlus;

import java.util.Map;

/**
 * @author : SandKing
 * @CreateDate : 2020/3/14 19:57
 * @Description ：Please describe this document
 */
public class MongoPlugin implements IPlugin {

    private MongoDS mongoDS = null;
    
    private String name;
    
    @Override
    public boolean start() {
        Map config = IServer.me().getContext().getConfig();
        Map mongo = MapPlus.get("plugins.mongo",config);

        Setting setting = new Setting();
        mongo.forEach((k,v)->{
            setting.put(k.toString(),v.toString());
        });

        this.mongoDS = MongoFactory.getDS(setting);
        this.name = MapUtil.getStr(mongo,"name");
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
    
    public MongoDatabase getMongoDatabase() {
    	return mongoDS.getDb(this.name);
    }
}
